﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
namespace Exact_Extension
{
    public partial class MainForm : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        public MainForm()
        {
            InitializeComponent();
            DS = new ExactDataContext();
            DS.Connection.ConnectionString = this.CheckConnStrFile();
            
            
        }

        private string CheckConnStrFile()
        {
            FileInfo fileConn = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\ConnExact.txt");
            string connStr = "";
            if (!fileConn.Exists)
            {
                StreamWriter writer = fileConn.CreateText();
                writer.WriteLine("CONNSTR|Data Source=isit-exac;Initial Catalog=200;Integrated Security=True");
             
                writer.Flush();
                writer.Close();

               
                StreamReader reader = fileConn.OpenText();
                while (reader.Peek() > 0)
                {
                    string text = reader.ReadLine();
                    connStr = text.Split(new char[1] { '|' })[1];
                }
                
            }
            else
            {
              
                StreamReader reader = fileConn.OpenText();
                while (reader.Peek() > 0)
                {
                    string text = reader.ReadLine();
                    connStr = text.Split(new char[1] { '|' })[1];
                }
            }
            return connStr;
            
        }
        private XtraForm CheckExistChildForm(Type formType)
        {
            foreach (XtraForm f in this.MdiChildren)
            {
                if (f.GetType() == formType)
                    return f;
            }
            return null;
        }
       
     
        private void btnTuitionFee_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(InitializeTFInvoice));
            if (frm != null)
                frm.Activate();
            else
            {
                InitializeTFInvoice frm1 = new InitializeTFInvoice(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

    

        private void btnDiscountMList_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(DiscountMaster));
            if (frm != null)
                frm.Activate();
            else
            {
                DiscountMaster frm1 = new DiscountMaster(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnDiscountAllocation_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(Discount));
            if (frm != null)
                frm.Activate();
            else
            {
                Discount frm1 = new Discount(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnCashReceipt_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(ImportCashBank));
            if (frm != null)
                frm.Activate();
            else
            {
                ImportCashBank frm1 = new ImportCashBank(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnDebtorRegister_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(DebtorRegisterForm));
            if (frm != null)
                frm.Activate();
            else
            {
                DebtorRegisterForm frm1 = new DebtorRegisterForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnCOETransfer_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(COETransfer));
            if (frm != null)
                frm.Activate();
            else
            {
                COETransfer frm1 = new COETransfer(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

       

        private void btnOtherFee_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(InitializeOTHFInvoice));
            if (frm != null)
                frm.Activate();
            else
            {
                InitializeOTHFInvoice frm1 = new InitializeOTHFInvoice(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnRevenueAllocation_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(ImportFinancialEntry));
            if (frm != null)
                frm.Activate();
            else
            {
                ImportFinancialEntry frm1 = new ImportFinancialEntry(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void btnDebtorList_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(DebtorListForm));
            if (frm != null)
                frm.Activate();
            else
            {
                DebtorListForm frm1 = new DebtorListForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnReminder_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(SendEmailForm));
            if (frm != null)
                frm.Activate();
            else
            {
                SendEmailForm frm1 = new SendEmailForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        

        private void btnPrintVATInvoice_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(RedInvoiceForm));
            if (frm != null)
                frm.Activate();
            else
            {
                RedInvoiceForm frm1 = new RedInvoiceForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnPrintVATOutput_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(TaxDeclareForm));
            if (frm != null)
                frm.Activate();
            else
            {
                TaxDeclareForm frm1 = new TaxDeclareForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnRevAllocate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(RevenueAllocateForm));
            if (frm != null)
                frm.Activate();
            else
            {
                RevenueAllocateForm frm1 = new RevenueAllocateForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnSendEmailInv_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(SendProformaInvForm));
            if (frm != null)
                frm.Activate();
            else
            {
                SendProformaInvForm frm1 = new SendProformaInvForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnARInformation_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(ARInformationForm));
            if (frm != null)
                frm.Activate();
            else
            {
                ARInformationForm frm1 = new ARInformationForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnRevAllocationSum_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(RevenueAllocateForm));
            if (frm != null)
                frm.Activate();
            else
            {
                RevenueAllocateForm frm1 = new RevenueAllocateForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnRevAllocationDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(RevenueAllocateDetailForm));
            if (frm != null)
                frm.Activate();
            else
            {
                RevenueAllocateDetailForm frm1 = new RevenueAllocateDetailForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnInvoiceList_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            XtraForm frm = this.CheckExistChildForm(typeof(InvoiceListForm));
            if (frm != null)
                frm.Activate();
            else
            {
                InvoiceListForm frm1 = new InvoiceListForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnInvoiceListSource_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(InvoiceListSourceForm));
            if (frm != null)
                frm.Activate();
            else
            {
                InvoiceListSourceForm frm1 = new InvoiceListSourceForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

       
    }
}